

// 读取html文件
const fs = require('fs')

function readError(res,err){
    res.writeHead(200,{'content-Type':'text/html;charset=utf8'})
    res.write('<h1>文件读取失败 '+err+'</h1>');
    res.end()
}

exports.readHTML = function(path,req,res){
    fs.readFile(path,"utf8",(err,data)=>{
        if(err){
            readError(res,err)
            throw err;
        }
        res.writeHead(200,{'content-Type':'text/html;charset=utf8'})
        res.write(data)
        res.end()
    })
}

exports.readJSON = function(path,req,res){
    fs.readFile(path,"utf8",(err,data)=>{
        if(err){
            readError(res,err)
            throw err;
        }
        res.writeHead(200,{"content-Type":'application/json'})
        res.write(data)
        res.end()
    })
}

exports.readIMG = function(path,req,res){
    fs.readFile(path,"binary",(err,data)=>{
        if(err){
            readError(res,err)
            throw err;
        }
        res.writeHead(200,{"content-Type":'image/jpeg'})  // 读取图片 
        res.write(data,'binary')
        res.end()
    })
}